package com.x9space.framework.elastic.handler;

import org.elasticsearch.index.query.QueryBuilder;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public abstract class QueryHandler {



   public abstract String getName();

   public QueryBuilder  doHandle(Annotation builder, Field f, Object o){

       boolean accessible = f.isAccessible();
       f.setAccessible(true);

       try {
           Object temp = f.get(o);
           if (temp == null){
              return null;
           } else {
               HashMap<String, Object> stringObjectHashMap = new HashMap<>();
               stringObjectHashMap.put("field",f);
               stringObjectHashMap.put("source",o);
               stringObjectHashMap.put("value",temp);
               return _doHandle(builder,stringObjectHashMap);
           }
       } catch (IllegalAccessException e) {
           return null;
       } finally {
           f.setAccessible(accessible);
       }
   }

   protected abstract QueryBuilder  _doHandle(Annotation annotation, Map<String,Object> map);

}
